package com.hgc.ch04.rw;

/**
 * Created by 01367627 on 2019/7/10.
 */
public class GoodsInfoServiceSyncImpl implements IGoodsInfoService {

    private GoodsInfo goodsInfo;

    public GoodsInfoServiceSyncImpl(GoodsInfo goodsInfo) {
        this.goodsInfo = goodsInfo;
    }

    @Override
    public synchronized void getNum() {
        System.out.println(Thread.currentThread().getName() + "读到数据为：" + goodsInfo.getNum());
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public synchronized void setNum(int num) {
        goodsInfo.setNum(num);
        System.out.println(Thread.currentThread().getName() + "写入数据：" + num);
        try {
            Thread.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
